<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
body .hll { background-color: #ffffcc }
body  { background: #ffffff; }
body .c { color: #008000 } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #0000ff } /* Keyword */
body .ch { color: #008000 } /* Comment.Hashbang */
body .cm { color: #008000 } /* Comment.Multiline */
body .cp { color: #0000ff } /* Comment.Preproc */
body .cpf { color: #008000 } /* Comment.PreprocFile */
body .c1 { color: #008000 } /* Comment.Single */
body .cs { color: #008000 } /* Comment.Special */
body .ge { font-style: italic } /* Generic.Emph */
body .gh { font-weight: bold } /* Generic.Heading */
body .gp { font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { font-weight: bold } /* Generic.Subheading */
body .kc { color: #0000ff } /* Keyword.Constant */
body .kd { color: #0000ff } /* Keyword.Declaration */
body .kn { color: #0000ff } /* Keyword.Namespace */
body .kp { color: #0000ff } /* Keyword.Pseudo */
body .kr { color: #0000ff } /* Keyword.Reserved */
body .kt { color: #2b91af } /* Keyword.Type */
body .s { color: #a31515 } /* Literal.String */
body .nc { color: #2b91af } /* Name.Class */
body .ow { color: #0000ff } /* Operator.Word */
body .sa { color: #a31515 } /* Literal.String.Affix */
body .sb { color: #a31515 } /* Literal.String.Backtick */
body .sc { color: #a31515 } /* Literal.String.Char */
body .dl { color: #a31515 } /* Literal.String.Delimiter */
body .sd { color: #a31515 } /* Literal.String.Doc */
body .s2 { color: #a31515 } /* Literal.String.Double */
body .se { color: #a31515 } /* Literal.String.Escape */
body .sh { color: #a31515 } /* Literal.String.Heredoc */
body .si { color: #a31515 } /* Literal.String.Interpol */
body .sx { color: #a31515 } /* Literal.String.Other */
body .sr { color: #a31515 } /* Literal.String.Regex */
body .s1 { color: #a31515 } /* Literal.String.Single */
body .ss { color: #a31515 } /* Literal.String.Symbol */

  </style>
</head>
<body>
<h2></h2>

<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt</span>
<span class="c1">#</span>
<span class="c1">#   This example shows how faces were jittered and augmented to create training</span>
<span class="c1">#   data for dlib&#39;s face recognition model.  It takes an input image and</span>
<span class="c1">#   disturbs the colors as well as applies random translations, rotations, and</span>
<span class="c1">#   scaling.</span>

<span class="c1">#</span>
<span class="c1"># COMPILING/INSTALLING THE DLIB PYTHON INTERFACE</span>
<span class="c1">#   You can install dlib using the command:</span>
<span class="c1">#       pip install dlib</span>
<span class="c1">#</span>
<span class="c1">#   Alternatively, if you want to compile dlib yourself then go into the dlib</span>
<span class="c1">#   root folder and run:</span>
<span class="c1">#       python setup.py install</span>
<span class="c1">#</span>
<span class="c1">#   Compiling dlib should work on any operating system so long as you have</span>
<span class="c1">#   CMake installed.  On Ubuntu, this can be done easily by running the</span>
<span class="c1">#   command:</span>
<span class="c1">#       sudo apt-get install cmake</span>
<span class="c1">#</span>
<span class="c1">#   Also note that this example requires Numpy which can be installed</span>
<span class="c1">#   via the command:</span>
<span class="c1">#       pip install numpy</span>
<span class="c1">#</span>
<span class="c1">#   The image file used in this example is in the public domain:</span>
<span class="c1">#   https://commons.wikimedia.org/wiki/File:Tom_Cruise_avp_2014_4.jpg</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">import</span> <span class="nn">dlib</span>

<span class="k">def</span> <span class="nf">show_jittered_images</span><span class="p">(</span><span class="n">window</span><span class="p">,</span> <span class="n">jittered_images</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        Shows the specified jittered images one by one</span>
<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">for</span> <span class="n">img</span> <span class="ow">in</span> <span class="n">jittered_images</span><span class="p">:</span>
        <span class="n">window</span><span class="o">.</span><span class="n">set_image</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
        <span class="n">dlib</span><span class="o">.</span><span class="n">hit_enter_to_continue</span><span class="p">()</span>

<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span>
        <span class="s2">&quot;Call this program like this:</span><span class="se">\n</span><span class="s2">&quot;</span>
        <span class="s2">&quot;   ./<a href="face_jitter.py.html">face_jitter.py</a> shape_predictor_5_face_landmarks.dat</span><span class="se">\n</span><span class="s2">&quot;</span>
        <span class="s2">&quot;You can download a trained facial shape predictor from:</span><span class="se">\n</span><span class="s2">&quot;</span>
        <span class="s2">&quot;    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="nb">exit</span><span class="p">()</span>

<span class="n">predictor_path</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">face_file_path</span> <span class="o">=</span> <span class="s2">&quot;../examples/faces/Tom_Cruise_avp_2014_4.jpg&quot;</span>

<span class="c1"># Load all the models we need: a detector to find the faces, a shape predictor</span>
<span class="c1"># to find face landmarks so we can precisely localize the face</span>
<span class="n">detector</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">get_frontal_face_detector</span><span class="p">()</span>
<span class="n">sp</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">shape_predictor</span><span class="p">(</span><span class="n">predictor_path</span><span class="p">)</span>

<span class="c1"># Load the image using dlib</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">load_rgb_image</span><span class="p">(</span><span class="n">face_file_path</span><span class="p">)</span>

<span class="c1"># Ask the detector to find the bounding boxes of each face.</span>
<span class="n">dets</span> <span class="o">=</span> <span class="n">detector</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>

<span class="n">num_faces</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">dets</span><span class="p">)</span>

<span class="c1"># Find the 5 face landmarks we need to do the alignment.</span>
<span class="n">faces</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">full_object_detections</span><span class="p">()</span>
<span class="k">for</span> <span class="n">detection</span> <span class="ow">in</span> <span class="n">dets</span><span class="p">:</span>
    <span class="n">faces</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sp</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">detection</span><span class="p">))</span>

<span class="c1"># Get the aligned face image and show it</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">get_face_chip</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">faces</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">size</span><span class="o">=</span><span class="mi">320</span><span class="p">)</span>
<span class="n">window</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">image_window</span><span class="p">()</span>
<span class="n">window</span><span class="o">.</span><span class="n">set_image</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="n">dlib</span><span class="o">.</span><span class="n">hit_enter_to_continue</span><span class="p">()</span>

<span class="c1"># Show 5 jittered images without data augmentation</span>
<span class="n">jittered_images</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">jitter_image</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">num_jitters</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="n">show_jittered_images</span><span class="p">(</span><span class="n">window</span><span class="p">,</span> <span class="n">jittered_images</span><span class="p">)</span>

<span class="c1"># Show 5 jittered images with data augmentation</span>
<span class="n">jittered_images</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">jitter_image</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">num_jitters</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">disturb_colors</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">show_jittered_images</span><span class="p">(</span><span class="n">window</span><span class="p">,</span> <span class="n">jittered_images</span><span class="p">)</span>
</pre></div>
</body>
</html>
